package com.kaigejava.springcloud.controller;

import com.kaigejava.springcloud.entities.CommonResult;
import com.kaigejava.springcloud.entities.Payment;
import com.kaigejava.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kaigejava
 * controller
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {

    @Value("${server.port}")
    private Integer serverPort;

    @Autowired
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("当前服务的端口是:{},插入数据的ID:{} \t" ,serverPort, payment.getId());
        log.info("插入结果：{}" + result);
        if (result > 0) {
            return new CommonResult(200, "当前服务端口为:"+serverPort+"插入数据成功:{}" , result);
        } else {
            return new CommonResult(444, "当前服务端口为:"+serverPort+"插入数据失败", null);
        }
    }

    @GetMapping("/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("当前服务的端口是:{}***查询结果：{}",serverPort, payment);
        if (payment != null) {
            return new CommonResult(200, "当前服务端口为:"+serverPort+"查询数据成功" , payment);
        } else {
            return new CommonResult(444, "当前服务端口为:"+serverPort+"没有对应记录", null);
        }
    }
    @GetMapping("/getDiscoveryClient")
    public Map<String,Object> getDiscoveryClient(){
        Map<String,Object> resutMap = new HashMap<>();
        //获取注册中心注册的服务列表。对应的就是Application
        List<String> applicationList = discoveryClient.getServices();
        List<String> applicationNameList = new ArrayList<>();
        for (String applicationName:applicationList ) {
            applicationNameList.add(applicationName);
            log.info("==获取到的服务名字是:\t"+applicationName +"\r\n");
        }
        //获取每个服务的提供者。对应的就是Application的status
        for(String applicationName:applicationNameList){
            List<ServiceInstance> instanceList = discoveryClient.getInstances(applicationName);
            List<String>  instanceNameList = new ArrayList<>();
            for(ServiceInstance serviceInstance:instanceList){
                String str = "实例名称:"+serviceInstance.getServiceId() +"\t Host: "+serviceInstance.getHost()
                        +"\t 端口号:"+serviceInstance.getPort() +"\t uri:"+serviceInstance.getUri();
                log.info(applicationName+"服务上获取到实例信息"+str);
                instanceNameList.add(str);
            }
            resutMap.put(applicationName+"服务上获取到实例信息",instanceNameList);
        }
        resutMap.put("获取到服务列表",applicationNameList);
        return resutMap;
    }


    @GetMapping("/zipkin")
    public String paymentZipkin() {
        log.info("===热部署测试==");
        return "hellp zipkin";
    }
}
